package com.ruoyi.am.controller;

import com.ruoyi.am.utils.SocketThread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

@Component
public class AmServer {

    @Autowired
    private AmRosterController AmRoster;

    private int driver_id = -1;

    private ServerSocket server = null;

    public static void main(String[] args) {
        new AmServer().server();
    }

    public void start(){
        server();
    }

    public void stop(){
        server = null;
    }

    public void server(){
        try {
            System.out.println("LISTEN SUCCESS!");
            // 创建一个ServerSocket在端口4700监听客户请求
            server = new ServerSocket(4700);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            // 使用accept阻塞等待客户请求，有客户请求来到，则产生一个Socket对象，并继续执行
            while (true) {
                Socket socket = server.accept();
                System.out.println("one client connect");
                SocketThread thread = new SocketThread(socket);
                thread.run();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
